<%@page import="helpers.StringHelper"%>
<%@page import="model.Paginate"%>
<%@page import="helpers.UrlHepler"%>
<%@page import="model.SearchResult"%>
<%@page import="model.RealEstate"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String key = (String) request.getAttribute("key");
    SearchResult sr = (SearchResult) request.getAttribute("searchResult");
    ArrayList<RealEstate> realEstates = (ArrayList<RealEstate>) sr.getListRealEstate();
    Paginate paginate = (Paginate) request.getAttribute("paginate");
    int start = (paginate.getCurrentPage() - 1) * paginate.getLimit() + 1;
    int end = paginate.getCurrentPage() * paginate.getLimit();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="/js/jquery-1.7.1.min.js"></script>
<title>Search Result</title>
<script type="text/javascript">
<!--
$(document).ready(function () {
	
});
//-->
</script>
</head>
<body>
    <form action="/result" method="get">
        <label>Key</label>
        <input type="text" name="key" value="<%=key%>">
        <button type="submit">Search</button>
    </form>
    <hr />
    <table>
        <tr>
            <td></td>
            <td>
                <h4>Ket qua tu <%=start %> den <%=end %> trong <%=sr.getTotalResult() %> ket qua (<%=sr.getQueryTime() %>)</h4>
                
                <div class="results">
                	<div class="items">
                    <%
                    RealEstate re = new RealEstate();
                    String sitename = "";
                    for (int i = 0; i < realEstates.size(); i++) {
                    	re = realEstates.get(i);
                    	sitename = UrlHepler.getSiteName(re.getUrl());
                    %>
                    	<div class="item">
	                        <div class="title"><a href="<%=re.getUrl()%>" target="_blank"><%=re.getTitle() %></a></div>
	                        <div><%=re.getTypeService() + ", " + re.getTypeRealEstate().toLowerCase()%> - <%=sitename%> - Ngay dang tin: <%=re.getDate()%></div>
	                        <br />
	                        <div class="description"><%=StringHelper.cutString(re.getDescription(), 100) %> ...</div>
	                        <div class="other">
	                        	<a href="#"><%=re.getDistrict() %></a>, <a href="#"><%=re.getProvince()%></a> - 
	                        	<%=re.getPrice()%> - <%=re.getContactPhone()%>
	                        </div>
                        </div>
                        <br />
                        <hr />
                    <%
                    }
                    %>
                    </div>
                </div>
                
                <div class="paginate">
                	<%
                	for (int i = paginate.getStart(); i < paginate.getEnd(); i++) {
                		if (i != paginate.getCurrentPage()) {
                	%>
                	<a href="/result?key=<%=key.replace(" ", "+")%>&page=<%=i%>"><%=i%></a> | 
                	<%
                		} else {
                	%>
                	<span><%=i%></span> | 
                	<%		
                		}
                	}
                	%>
                	<a href="<%=paginate.getNext()%>">Trang tiep ...</a>
                </div>
            </td>
        </tr>
    </table>
</body>
</html>